<?php

class yunuser extends IController
{
    private $user_id = 0;

    public function init()
    {
        $user_id = IFilter::act(IReq::get('user_id'), 'int');
//        if(!$user_id){
//            Yun::ajaxReturn(1,'无效的用户ID参数');
//            exit();
//        }
        $this->user_id = $user_id ? $user_id : $this->user['user_id'];
    }

    /**
     * 首页初始化会员信息
     */
    public function get_user_index(){
        //获取用户基本信息
        $user = Api::run('getMemberInfo',$this->user_id);
        if($user){
            //成功
            /*获取不同状态的订单数量统计*/
            //待付款
            $countUserWaitPay = statistics::countUserWaitPay($this->user_id);
            //待发货
            $orderDB = new IModel('order');
            $data    = $orderDB->getObj('user_id = '.$this->user_id.' and status = 2 and distribution_status = 0 and if_del = 0','count(id) as num');
            $countUserWaitFa = $data['num'];
            //待收货
            $countUserWaitCommit = statistics::countUserWaitCommit($this->user_id);
            //售后
            $re_model = new IModel('refundment_doc');
            $data1    = $re_model->getObj('user_id = '.$this->user_id.' and pay_status = 0 and if_del = 0','count(id) as num');
            $countUserWaitAfter = $data1['num'];
            $data = array(
                'true_name' => $user['true_name'],
                'head_ico' => $user['head_ico'],
                'birthday' => $user['birthday'],
                'sex' => $user['sex'],
                'sex_name' => $user['sex'] == 1 ? '男' : '女',
                'countUserWaitPay' => $countUserWaitPay ? $countUserWaitPay : 0,
                'countUserWaitFa' => $countUserWaitFa ? $countUserWaitFa : 0,
                'countUserWaitCommit' => $countUserWaitCommit ? $countUserWaitCommit : 0,
                'countUserWaitAfter' => $countUserWaitAfter ? $countUserWaitAfter : 0);
            Yun::ajaxReturn(0, '成功', $data);
        }else{
            //失败
            Yun::ajaxReturn(1, '未查询到相关数据');
        }
    }

    /**
     * 编辑页面初始化会员信息
     */
    public function get_user_info(){
        //获取用户基本信息
        $user = Api::run('getMemberInfo',$this->user_id);
        if($user['user_id']){
            //成功
            $user['head_ico'] = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $user['head_ico'];
            Yun::ajaxReturn(0, '成功', $user);
        }else{
            //失败
            Yun::ajaxReturn(1, '未查询到相关数据');
        }
    }

    /**
     * 地址初始化
     */
    public function get_address(){
        //取得自己的地址
        $query = new IQuery('address');
        $query->where = 'user_id = '.$this->user_id;
        $address = $query->find();
        if($address)
        {
            //默认地址的自我修正
            Yun::fixAddressDefault($address);
            foreach($address as &$ad)
            {
                $temp = area::name($ad['province'],$ad['city'],$ad['area']);
                if(isset($temp[$ad['province']]) && isset($temp[$ad['city']]) && isset($temp[$ad['area']]))
                {
                    $ad['province_val'] = $temp[$ad['province']];
                    $ad['city_val']     = $temp[$ad['city']];
                    $ad['area_val']     = $temp[$ad['area']];
                }
            }
            Yun::ajaxReturn(0, '成功', $address);
        }else{
            Yun::ajaxReturn(1, '未查询到相关数据');
        }
    }

    /**
     * 获取所有的省份
     */
    public function get_provinces(){
        $query = new IQuery('areas');
        $query->where = "parent_id = 0";
        $provinces = $query->find();
        if($provinces){
            Yun::ajaxReturn(0, '成功', $provinces);
        }else{
            Yun::ajaxReturn(1, '未查询到相关数据');
        }
    }

    /**
     * 根据省份ID获取所属城市
     */
    public function get_citys(){
        $province_id = IFilter::act(IReq::get('province_id'), 'int');
        if(!$province_id){
            Yun::ajaxReturn(1, '省份ID不能为空');
        }
        $query = new IQuery('areas');
        $query->where = "parent_id = " . $province_id;
        $citys = $query->find();
        if($citys){
            Yun::ajaxReturn(0, '成功', $citys);
        }else{
            Yun::ajaxReturn(1, '未查询到相关数据');
        }
    }

    /**
     * 根据城市获得区域
     */
    public function get_areas(){
        $city_id = IFilter::act(IReq::get('city_id'), 'int');
        if(!$city_id){
            Yun::ajaxReturn(1, '城市ID不能为空');
        }
        $query = new IQuery('areas');
        $query->where = "parent_id = " . $city_id;
        $areas = $query->find();
        if($areas){
            Yun::ajaxReturn(0, '成功', $areas);
        }else{
            Yun::ajaxReturn(1, '未查询到相关数据');
        }
    }

    /**
     * 收货地址的添加与编辑
     */
    public function address_edit(){
        $id          = IFilter::act(IReq::get('id'),'int');
        $accept_name = IFilter::act(IReq::get('accept_name'),'name');
        $province    = IFilter::act(IReq::get('province'),'int');
        $city        = IFilter::act(IReq::get('city'),'int');
        $area        = IFilter::act(IReq::get('area'),'int');
        $address     = IFilter::act(IReq::get('address'));
        $zip         = IFilter::act(IReq::get('zip'),'zip');
        $telphone    = IFilter::act(IReq::get('telphone'),'phone');
        $mobile      = IFilter::act(IReq::get('mobile'),'mobile');
        $default     = IReq::get('is_default')!= 1 ? 0 : 1;
        $user_id     = $this->user_id;

        if(!$accept_name){
            Yun::ajaxReturn(1, '收货人名称必须传递');
        }
        if(!$province){
            Yun::ajaxReturn(1, '省份ID必须传递');
        }
        if(!$city){
            Yun::ajaxReturn(1, '城市ID必须传递');
        }
        if(!$area){
            Yun::ajaxReturn(1, '区域ID必须传递');
        }
        if(!$address){
            Yun::ajaxReturn(1, '具体地址信息必须传递');
        }
        if(!$mobile){
            Yun::ajaxReturn(1, '手机号码必须传递');
        }

        if(!preg_match('/^1[34578]\d{9}$/', $mobile)){
            Yun::ajaxReturn(1, '手机号码格式错误');
        }

        $model = new IModel('address');
        $data  = array('user_id'=>$user_id,'accept_name'=>$accept_name,'province'=>$province,'city'=>$city,'area'=>$area,'address'=>$address,'zip'=>$zip,'telphone'=>$telphone,'mobile'=>$mobile,'is_default'=>$default);

        //如果设置为首选地址则把其余的都取消首选
        if($default==1)
        {
            $model->setData(array('is_default' => 0));
            $model->update("user_id = ".$user_id);
        }

        $model->setData($data);

        if($id == '')
        {
            $id = $model->add();
            if($id){
                Yun::ajaxReturn(0, '地址添加成功');
            }else{
                Yun::ajaxReturn(1, '地址添加失败');
            }
        }
        else
        {
            $bool = $model->update('id = '.$id);
            if($bool){
                Yun::ajaxReturn(0, '地址修改成功');
            }else{
                Yun::ajaxReturn(1, '地址修改失败');
            }
        }
    }

    /**
     * 地址删除
     */
    public function address_del()
    {
        $id = IFilter::act( IReq::get('id'),'int' );
        if(!$id){
            Yun::ajaxReturn(1, '无效的ID参数');
        }
        $model = new IModel('address');
        $bool = $model->del('id = '.$id.' and user_id = '.$this->user_id);
        if($bool){
            Yun::ajaxReturn(0, '地址删除成功');
        }else{
            Yun::ajaxReturn(1, '地址删除失败');
        }
    }

    /**
     * 设置默认的收货地址
     */
    public function address_default()
    {
        $id = IFilter::act(IReq::get('id'),'int');
        if(!$id){
            Yun::ajaxReturn(1, '无效的ID参数');
        }
//        $default = IFilter::act($_REQUEST['is_default']);
        $default = 1;
        $model = new IModel('address');
        if($default == 1)
        {
            $model->setData(array('is_default' => 0));
            $model->update("user_id = ".$this->user_id);
        }
        $model->setData(array('is_default' => $default));
        $bool = $model->update("id = ".$id." and user_id = ".$this->user_id);
        if($bool){
            Yun::ajaxReturn(0, '设置默认地址成功');
        }else{
            Yun::ajaxReturn(1, '设置默认地址失败');
        }
    }

    /**
     * 获取订单
     */
    public function get_orders(){
        $page = isset($_REQUEST['page']) ? IFilter::act($_REQUEST['page'], 'int') : 1;
        $pageSize = isset($_REQUEST['pagesize']) ? IFilter::act($_REQUEST['pagesize'], 'int') : 20;
        $status = isset($_REQUEST['status']) ? $_REQUEST['status'] : '全部';
        //获取订单数据
        $orders = $this->getOrderList($this->user_id, $page, $pageSize, $status);
        if($orders){
            Yun::ajaxReturn(0, '成功', $orders);
        }else{
            Yun::ajaxReturn(1, '未查询到相关数据');
        }
    }

    /**
     * 查看订单详情
     */
    public function order_detail()
    {
        $id = isset($_REQUEST['id']) ? IFilter::act($_REQUEST['id'], 'int') : 0;
        $orderObj = new order_class();
        $order_info = $orderObj->getOrderShow($id, $this->user_id);
        if(!$order_info){
            Yun::ajaxReturn(1, '订单信息不存在');
        }else{
            $orderStatus = Order_Class::getOrderStatus($order_info);
            $order_info['status_name'] = Order_Class::orderStatusText($orderStatus);
            $refundDB  = new IModel('refundment_doc');
            $refundRow = $refundDB->getObj('order_no = "'.$order_info['order_no'].'" and if_del = 0 and pay_status = 0');
            $order_info['refundRow'] = $refundRow;
            $order_info['prop_info'] = array();
            if($order_info['prop']){
                $prop_model= new IModel('prop');
                $prop_info = $prop_model->getObj("id = " . $order_info['prop']);
                $order_info['prop_info'] = $prop_info;
            }
            $order_goods_q = new IModel('order_goods');
            $goods = $order_goods_q->getObj("order_id=".$order_info['id']);
            $goods['goods_array'] = json_decode($goods['goods_array'], TRUE);
            $order_info['goods_data'] = $goods;
            Yun::ajaxReturn(0, '成功', $order_info);
        }
    }

    /**
     * 订单状态操作
     */
    public function order_status()
    {
        $op    = IFilter::act(IReq::get('op'));
        $id    = IFilter::act(IReq::get('order_id'),'int' );
        $model = new IModel('order');

        switch($op)
        {
            case "cancel":
            {
                $model->setData(array('status' => 3));
                if($model->update("id = ".$id." and distribution_status = 0 and status = 1 and user_id = ".$this->user_id))
                {
                    order_class::resetOrderProp($id);
                }
                Yun::ajaxReturn(0, '取消订单成功');
            }
                break;

            case "confirm":
            {
                $model->setData(array('status' => 5,'completion_time' => ITime::getDateTime()));
                if($model->update("id = ".$id." and distribution_status = 1 and user_id = ".$this->user_id))
                {
                    $orderRow = $model->getObj('id = '.$id);
                    //确认收货后进行支付
                    Order_Class::updateOrderStatus($orderRow['order_no']);
                    //增加用户评论商品机会
                    Order_Class::addGoodsCommentChange($id);
                }
                Yun::ajaxReturn(0, '确认收货成功');
            }
                break;
        }
    }

    /**
     * 退款申请页面
     */
    public function refunds_update()
    {
        $order_goods_id = IFilter::act( IReq::get('order_goods_id'),'int' );
        $order_id       = IFilter::act( IReq::get('order_id'),'int' );
        $user_id        = $this->user_id;
        $content        = IFilter::act(IReq::get('content'),'text');
        $imgs           = $_REQUEST['imgs'];
        $order_goods_id = array($order_goods_id);
        $imgs = explode(',', $imgs);
        foreach ($imgs as $k => $v){
            $content .= '<p><img src=" '.$v .' " /> </p>';
        }

        if(!$content || !$order_goods_id)
        {
            Yun::ajaxReturn(1, '请填写退款理由和选择要退款的商品');
        }
        $orderDB      = new IModel('order');
        $orderRow     = $orderDB->getObj("id = ".$order_id." and user_id = ".$user_id);
        $refundResult = Order_Class::isRefundmentApply($orderRow,$order_goods_id);

        //判断退款申请是否有效
        if($refundResult === true){
            //退款单数据
            $updateData = array(
                'order_no'       => $orderRow['order_no'],
                'order_id'       => $order_id,
                'user_id'        => $user_id,
                'time'           => ITime::getDateTime(),
                'content'        => $content,
                'seller_id'      => $orderRow['seller_id'],
                'order_goods_id' => join(",",$order_goods_id),
            );

            //写入数据库
            $refundsDB = new IModel('refundment_doc');
            $refundsDB->setData($updateData);
            $id = $refundsDB->add();
            if($id){
                Yun::ajaxReturn(0, '申请退款成功');
            }else{
                Yun::ajaxReturn(1, '申请退款失败');
            }
        }else{
            Yun::ajaxReturn(1, $refundResult);
        }
    }

    /**
     * @brief 退款申请取消
     */
    public function refunds_del()
    {
        $id = IFilter::act(IReq::get('id'),'int' );
        if(!$id){
            Yun::ajaxReturn(1, '无效的ID参数');
        }
        $model = new IModel("refundment_doc");
        $bool = $model->del("id = ".$id." and user_id = ".$this->user_id);
        if ($bool){
            Yun::ajaxReturn(0, '退款申请取消成功');
        }else{
            Yun::ajaxReturn(1, '退款申请取消失败');
        }
    }

    /**
     * 快递跟踪
     */
    public function freight(){
        $id = IFilter::act(IReq::get('delivery_id'), 'int');
        if($id)
        {
            $tb_freight = new IQuery('delivery_doc as d');
            $tb_freight->join  = 'left join freight_company as f on f.id = d.freight_id';
            $tb_freight->where = 'd.id = '.$id;
            $tb_freight->fields= 'd.*,f.freight_type';
            $freightData = $tb_freight->find();

            if($freightData)
            {
                $freightData = current($freightData);
                if($freightData['freight_type'] && $freightData['delivery_code']) {
                    $result = freight_facade::line($freightData['freight_type'],$freightData['delivery_code']);
                    if($result['result'] == 'success')
                    {
                        Yun::ajaxReturn(0, '查询成功', $result['data']);
                    }
                    else
                    {
                        Yun::ajaxReturn(1, '物流接口发生错误');
                    }
                }else {
                    Yun::ajaxReturn(1, '缺少物流信息');
                }
            }
        }
        Yun::ajaxReturn(1, '发货单信息不存在');
    }

    /**
     * 获取代金券
     */
    public function get_redpacket(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
        $pageSize = isset($_REQUEST['pagesize']) ? $_REQUEST['pagesize'] : 20;
        $member_info = Api::run('getMemberInfo', $this->user_id);
        $propIds     = trim($member_info['prop'], ',');
        $propIds     = $propIds ? $propIds : 0;
        $query = new IQuery('prop');
        $query->where  = "id in(".$propIds.") and is_send = 1";
        $count = count($query->find());
        $query->page   = $page;
        $query->pagesize = $pageSize;
        $items = $query->find();
        if($items){
            $array = array('page' => array('page_total' => ceil($count/$pageSize), 'page' => $page, 'page_size' => $pageSize), 'listData' => $items);
            Yun::ajaxReturn(0, '成功', $array);
        }else{
            Yun::ajaxReturn(1, '未查询到相关数据');
        }
    }

    /**
     * 添加实体代金券
     */
    public function add_download_ticket(){

        $ticket_num = IReq::get('ticket_num');
        $ticket_pwd = IReq::get('ticket_pwd');
        if(!$ticket_num){
            Yun::ajaxReturn(1, '请输入兑换卡号');
        }
        if(!$ticket_pwd){
            Yun::ajaxReturn(1, '请输入兑换密码');
        }

        //代金券状态是否正常
        $propObj = new IModel('prop');
        $propRow = $propObj->getObj('card_name = "'.$ticket_num.'" and card_pwd = "'.$ticket_pwd.'" and type = 0 and is_userd = 0 and is_send = 1 and is_close = 0 and NOW() between start_time and end_time');
        if(!$propRow) {
            Yun::ajaxReturn(1, '代金券不可用，请确认代金券的卡号密码并且此代金券从未被使用过');
        }else{
            //代金券是否已经被领取
            $memberObj = new IModel('member');
            $isRev     = $memberObj->query('FIND_IN_SET('.$propRow['id'].',prop)');
            if($isRev) {
                Yun::ajaxReturn(1, '代金券已经被领取');
            }else{
                $memberRow = $memberObj->getObj('user_id = '.$this->user_id, 'prop');
                if($memberRow['prop'] == '')
                {
                    $propUpdate = ','.$propRow['id'].',';
                }
                else
                {
                    $propUpdate = $memberRow['prop'].$propRow['id'].',';
                }

                $dataArray = array('prop' => $propUpdate);
                $memberObj->setData($dataArray);
                $memberObj->update('user_id = '.$this->user_id);
                Yun::ajaxReturn(0, '绑定成功');
            }
        }
    }

    /**
     * 密码修改
     */
    public function password_edit(){
        $fpassword  = IReq::get('fpassword');
        $password   = IReq::get('password');
        if(!$fpassword){
            Yun::ajaxReturn(1, '请输入旧密码');
        }
        if(!$password){
            Yun::ajaxReturn(1, '请输入新密码');
        }

        $userObj    = new IModel('user');
        $where      = 'id = '.$this->user_id;
        $userRow    = $userObj->getObj($where);

        if(!preg_match('|\w{6,32}|',$password))
        {
            Yun::ajaxReturn(1, '密码格式不正确，请重新输入');
        }

        else if(md5($fpassword) != $userRow['password'])
        {
            Yun::ajaxReturn(1, '原始密码输入错误');
        }
        else
        {
            $passwordMd5 = md5($password);
            $dataArray = array(
                'password' => $passwordMd5,
            );

            $userObj->setData($dataArray);
            $result  = $userObj->update($where);
            if($result)
            {
                ISafe::set('user_pwd',$passwordMd5);
                Yun::ajaxReturn(0, '密码修改成功');
            }
            else
            {
                Yun::ajaxReturn(1, '密码修改失败');
            }
        }
    }

    /**
     * 用户上传图片
     */
    public function upload_pic(){
        if(isset($_FILES['attach']['name']) && $_FILES['attach']['name'] != ''){
            $photoObj = new PhotoUpload();
            $photo    = $photoObj->run();
            if($photo['attach']['img']){
                Yun::ajaxReturn(0, '上传成功', array('path' => $photo['attach']['img']));
            }else{
                Yun::ajaxReturn(1, '上传失败');
            }
        }else{
            Yun::ajaxReturn(1, '请选择图片');
        }
    }

    /**
     * 用户上传修改头像
     */
    public function user_ico_upload(){
        if(isset($_FILES['attach']['name']) && $_FILES['attach']['name'] != '')
        {
            $photoObj = new PhotoUpload();
            $photo    = $photoObj->run();

            if($photo['attach']['img'])
            {
                $user_id   = $this->user_id;
                $user_obj  = new IModel('user');
                $dataArray = array(
                    'head_ico' => $photo['attach']['img'],
                );
                $user_obj->setData($dataArray);
                $where  = 'id = '.$user_id;
                $isSuss = $user_obj->update($where);

                if($isSuss !== false)
                {
                    $result['isError'] = false;
                    $result['data'] = IUrl::creatUrl().$photo['attach']['img'];
                    ISafe::set('head_ico',$dataArray['head_ico']);
                    Yun::ajaxReturn(0, '上传成功', $dataArray);
                }
                else
                {
                    Yun::ajaxReturn(1, '上传失败');
                }
            }
            else
            {
                Yun::ajaxReturn(1, '上传失败');
            }
        }
        else
        {
            Yun::ajaxReturn(1, '请选择图片');
        }
    }

    /**
     * 个人资料的编辑
     */
    public function info_edit_act(){
        $user_id   = $this->user_id;
        $memberObj = new IModel('member');
        $where     = 'user_id = '.$user_id;
        $dataArray = array();

        if(isset($_REQUEST['true_name']))
            $dataArray['true_name'] = IFilter::act( $_REQUEST['true_name'] ,'string');

        if(isset($_REQUEST['sex']))
            $dataArray['sex'] = IFilter::act( IReq::get('sex'),'int' );

        if(isset($_REQUEST['birthday']))
            $dataArray['birthday'] = IFilter::act( IReq::get('birthday') );
        $memberObj->setData($dataArray);
        $bool = $memberObj->update($where);
        if($bool){
            Yun::ajaxReturn(0, '修改成功');
        }else{
            Yun::ajaxReturn(1, '修改失败');
        }
    }

    /**
     * 站点建议接口
     */
    public function complain_edit()
    {
        $content = IFilter::act(IReq::get('content'),'string' );
        $title = mb_substr($content, 0,15);
        $imgs           = $_REQUEST['imgs'];
        $imgs = explode(',', $imgs);
        if($imgs){
            foreach ($imgs as $k => $v){
                $content .= '<p><img src=" '.$v .' " /> </p>';
            }
        }
        $user_id = $this->user_id;
        $model = new IModel('suggestion');
        $model->setData(array('user_id'=>$user_id,'title'=>$title,'content'=>$content,'time'=>ITime::getDateTime()));
        $id = $model->add();
        if($id){
            Yun::ajaxReturn(0, '成功');
        }else{
            Yun::ajaxReturn(1, '失败');
        }
    }

    /**
     * 商家入驻接口
     */
    public function seller_join(){
        $name = IFilter::act(IReq::get('name'),'string' );
        $mobile = IFilter::act(IReq::get('mobile'));
        $main_goods = IFilter::act(IReq::get('main_goods'));
        if(!$name){
            Yun::ajaxReturn(1, '请填写联系人');
        }
        if(!$mobile){
            Yun::ajaxReturn(1, '请填写联系电话');
        }
        if(!$main_goods){
            Yun::ajaxReturn(1, '请填写主营产品');
        }
        $model = new IModel('seller_join');
        $model->setData(array('name' => $name, 'mobile' => $mobile,'main_goods' => $main_goods,'create_time' => time()));
        $id = $model->add();
        if($id){
            Yun::ajaxReturn(0, '成功');
        }else{
            Yun::ajaxReturn(1, '失败');
        }
    }

    /**
     * 私人定制接口
     */
    public function set_customize(){
        $name = IFilter::act(IReq::get('name'),'string' );
        $mobile = IFilter::act(IReq::get('mobile'));
        $content = IFilter::act(IReq::get('content'));
        $type = IFilter::act(IReq::get('type'));
        if(!$name){
            Yun::ajaxReturn(1, '请填写联系人');
        }
        if(!$mobile){
            Yun::ajaxReturn(1, '请填写联系电话');
        }
        if(!$content){
            Yun::ajaxReturn(1, '请填写具体要求');
        }
        $model = new IModel('customize');
        $model->setData(array('name' => $name, 'mobile' => $mobile,'content' => $content, 'type' => $type, 'create_time' => time()));
        $id = $model->add();
        if($id){
            Yun::ajaxReturn(0, '成功');
        }else{
            Yun::ajaxReturn(1, '失败');
        }
    }

    /**
     * @brief 初始化支付方式
     */
    private function initPayment()
    {
        $payment = new IQuery('payment');
        $payment->fields = 'id,name,type';
        $payments = $payment->find();
        $items = array();
        foreach($payments as $pay)
        {
            $items[$pay['id']]['name'] = $pay['name'];
            $items[$pay['id']]['type'] = $pay['type'];
        }
        return $items;
    }

    /**
     * 根据条件获取订单
     * @param $userid
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    private function getOrderList($userid, $page = 1, $pageSize = 20, $status){
        //初始化支付方式
        $payments = $this->initPayment();
        //判断是否有退款的中的订单
        $re_query = new IQuery('refundment_doc');
        $re_query->where = "user_id = " . $this->user_id . " and pay_status = 0 and if_del = 0";
        $re_query->fields = 'order_id';
        $re_result = $re_query->find();
        $re_result = array_column($re_result, 'order_id');
        $re_result_str = implode(',' , $re_result);
        $query = new IQuery('order');
        switch ($status){
            case 'pay':
                $query->where = "user_id =".$userid." and if_del= 0 and status = 1";
                break;
            case 'delivery':
                if($re_result){
                    $query->where = "user_id =".$userid." and if_del= 0 and status = 2 and distribution_status = 0 and id not in ({$re_result_str})";
                }else{
                    $query->where = "user_id =".$userid." and if_del= 0 and status = 2 and distribution_status = 0";
                }
                break;
            case 'receipt':
                if($re_result){
                    $query->where = "user_id =".$userid." and if_del= 0 and status = 2 and distribution_status = 1 and id not in ({$re_result_str})";
                }else{
                    $query->where = "user_id =".$userid." and if_del= 0 and status = 2 and distribution_status = 1";
                }
                break;
            case 'finished':
                $query->where = "user_id =".$userid." and if_del= 0 and id in ({$re_result_str})";
                break;
            default:
                $query->where = "user_id =".$userid." and if_del= 0";
                break;
        }
        $count = count($query->find());
        $query->order = "id desc";
        $query->page  = $page;
        $query->pagesize = $pageSize;
        $orders = $query->find();
        if ($orders){
            $order_goods_q = new IModel('order_goods');
            $seller_model = new IModel('seller');
            foreach ($orders as &$order){
                $seller_data = $seller_model->getObj('id = ' . $order['seller_id']);
                $order['seller_name'] = $seller_data['true_name'];
                $order['status_name'] = Order_Class::orderStatusText(Order_Class::getOrderStatus($order));
                $order['pay_type_name'] = $payments[$order['pay_type']]['name'];
                $goods = $order_goods_q->getObj("order_id=".$order['id']);
                $goods_json = json_decode($goods['goods_array'], TRUE);
                $order['goods_data'] = array('name' => $goods_json['name'], 'img' => $goods['img'], 'real_price' => $goods['real_price']);
            }
            $array = array('page' => array('page_total' => ceil($count/$pageSize), 'page' => $page, 'page_size' => $pageSize), 'listData' => $orders);
        }else{
            $array = array();
        }
        return $array;
    }
}